import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cwhisky/common/style/colors.dart';
import 'package:flutter_cwhisky/component/image_view.dart';
import 'package:flutter_cwhisky/component/text_view.dart';
import 'package:flutter_cwhisky/store/user_state.dart';
import 'package:flutter_cwhisky/utils/log_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../bean/mall_home_entity.dart';

class MemberGood extends StatelessWidget {
  final MallGoods good;
  bool showLabel = true;
  bool isKnight = UserState.isKnight;
  String memberPrice = '--';

  MemberGood({Key? key, required this.good}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    _handlePrice();
    return Container(
      width: 102.r,
      margin: EdgeInsets.only(top: 12.r, left: 5.r, bottom: 15.h),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8.r),
          gradient: const LinearGradient(
              begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Color(0xffFFF9F1), Color(0xffFFEED9)])),
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.all(6.r),
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(6.r), color: AppColors.white),
            padding: EdgeInsets.symmetric(horizontal: 6.r, vertical: 2.r),
            child: ImageView(
              url: good.image ?? '',
              width: 78.r,
              height: 78.r,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(left: 12.r, right: 12.r, top: 6.r),
            child: TextView(
              text: good.cnName ?? '',
              fontSize: 12.sp,
              fontColor: AppColors.color332B29,
              fontWeight: FontWeight.w500,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(
              top: 4.r,
              left: 12.r,
              right: 12.r,
            ),
            child: Row(
              children: [
                TextView(
                  text: memberPrice,
                  fontColor: AppColors.color332B29,
                  fontSize: 10.sp,
                  fontWeight: FontWeight.w900,
                ),
                showLabel
                    ? Container(
                        margin: EdgeInsets.only(left: 6.r),
                        width: 30.r,
                        height: 14.r,
                        alignment: Alignment.center,
                        decoration: const BoxDecoration(color: AppColors.color332B29),
                        child: TextView(
                          text: '会员价',
                          fontSize: 8.sp,
                          fontColor: const Color(0xffEFD3A7),
                        ),
                      )
                    : const SizedBox(
                        width: 30,
                        height: 14,
                      )
              ],
            ),
          )
        ],
      ),
    );
  }

  _handlePrice() {
    //零售价
    var price = 0.0;
    var knightPrice = 0.0;
    if (good.showPriceType == 0) {
      price = good.price ?? 0.0;
    } else if (good.showPriceType == 1) {
      knightPrice = good.showPrice ?? 0.00;
      price = good.price ?? 0.00;
    } else if (good.showPriceType == 2) {
      //showPriceType==2会员价(showPrice为零售价，price为会员价)
      price = good.showPrice ?? 0.00;
      knightPrice = good.price ?? 0.00;
    }
    // 如果没有会员价 直接显示零售价
    if (good.showPriceType == 0 || knightPrice <= 0) {
      memberPrice = '¥$price';
      return;
    }
    if (isKnight) {
      if (knightPrice < price) {
        showLabel = true;
        memberPrice = '¥$knightPrice';
      } else {
        memberPrice = '¥$price';
      }
      // return `¥${Math.min(knightPrice, price)}`
    } else {
      // 会员价不为0 ,需要比对会员价和零售价的大小 ,哪个价格小显示哪一个
      if (knightPrice < price) {
        showLabel = true;
        memberPrice = '¥${getPrivatePrice(Decimal.parse(knightPrice.toString()).toString())}';
      } else {
        memberPrice = '¥$price';
      }
    }
  }

  String getPrivatePrice(String knightPrice) {
    var memberPrice = "$knightPrice";
    LogUtils.d(memberPrice);
    var price = StringBuffer();
    for (var i = 0; i < memberPrice.length; i++) {
      if (knightPrice.length > 5) {
        if (i >= 3) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      } else if (knightPrice.length > 3) {
        if (i >= 2) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      } else {
        if (i >= 1) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      }
    }
    return price.toString();
  }
}
